//
//  EmojiManage.m
//  TestNIMSDK
//
//  Created by jianyong.wang on 2017/6/1.
//  Copyright © 2017年 jianyong.wang. All rights reserved.
//

#import "JYEmojiManager.h"
@implementation Emoticon
@end
@implementation JYEmojiManager
+ (instancetype)sharedManager
{
    static JYEmojiManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[JYEmojiManager alloc]init];
    });
    return instance;
}
- (id)init
{
    if (self = [super init])
    {
        [self parsePlist];
    }
    return self;
}
-(void)parsePlist
{
    NSArray * array  = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"emoji" ofType:@"plist"]];
    NSDictionary * dict = array[0];
    NSArray <NSDictionary *>* dataArray= dict[@"data"];
    @autoreleasepool {
        [dataArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            Emoticon * moji = [[Emoticon alloc]init];
            moji.emoticonID = obj[@"id"];
            moji.filename = obj[@"file"];
            moji.tag = obj[@"tag"];
            [self.catalogs addObject:moji];
        }];
    }
    //填充collection 否则最后一个页面不会完整展现
    for (int i = 1; i < 20; i++) {
        Emoticon * moji = [[Emoticon alloc]init];
        moji.emoticonID = @"";
        moji.filename = @"";
        if (i < 6) {
            moji.tag = @"删除";
            moji.filename = @"emoji_del_normal";
            [self.catalogs insertObject:moji atIndex:(24*i-1)];
        }else{
            moji.filename = i==6 ?@"emoji_del_normal":@"";
            moji.tag = i==6 ?@"删除":@"";
            [self.catalogs addObject:moji];
        }
    }
}
#pragma mark - methods
//获取表情对应的Emoticon模型
- (Emoticon *)emoticonByTag:(NSString *)tag
{
    Emoticon *emoticon = nil;
    if ([tag length])
    {
        for (Emoticon *catalog in self.catalogs)
        {
            if ([catalog.tag isEqualToString:tag])
            {
                emoticon = catalog;
                break;
            }
        }
    }
    return emoticon;
}
//调用表情view删除键，返回'['的index
-(NSUInteger)rangOfLastEmotion:(NSString *)text
{
    NSInteger index = -1;
    for (long i = (text.length - 1); i >= 0; i--) {
        char suff = [text characterAtIndex:i];
        if (suff == '[') {
            index = i;
            break;
        }
    }
    return index ;
}
//删除选中字符串
- (void)deleteString:(UITextView *)textView {
    NSRange range = textView.selectedRange;
    if (textView.text.length > 0) {
        NSUInteger location  = textView.selectedRange.location;
        if (location > 0) {
            NSMutableString *str = [NSMutableString stringWithFormat:@"%@",textView.text];
            NSLog(@"%zd===%zd",[self lastRange:str range:range].location,[self lastRange:str range:range].length);
            NSLog(@"%@",str);
            [str deleteCharactersInRange:[self lastRange:str range:range]];
            NSLog(@"%@",str);
            textView.text = str;
            textView.selectedRange = NSMakeRange(str.length,0);
        }
    }
}
//获取[]的下标索引，由于JYTextView中已经判断了表情字符串 如 [大笑] 的完整性，故此处不需要判断
- (NSRange)lastRange:(NSString *)text range:(NSRange)range{
    NSUInteger endLocation = range.location -1;
    NSUInteger index = -1;
    for (long i = endLocation; i >= 0; i --) {
        char suff = [text characterAtIndex:i];
        if (suff == '[') {
            index = i;
            break;
        }
    }
    return NSMakeRange(index, text.length - index);
}

#pragma mark - lazy
-(NSArray *)catalogs
{
    if (_catalogs == nil) {
        _catalogs = [NSMutableArray array];
    }
    return _catalogs;
}
@end
